package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.server.service.CommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigurationPackage;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.ws.rs.Path;
import java.util.Map;

@RestController
@RequestMapping("/comments")
public class CommentsController {

    @Autowired
    private CommentsService commentsService;

    @GetMapping//查询所有评论
    public ResponseEntity comments(@RequestParam(defaultValue = "1")Integer page,
                                   @RequestParam(defaultValue = "10")Integer pagesize,
                                   String movementId){
        //1.获取参数

        //2.调用service处
        PageResult vo=commentsService.findComments(movementId,page,pagesize);
        //3.返回参数
        return ResponseEntity.ok(vo);
    }


    @PostMapping//发布评论
    public ResponseEntity saveComment(@RequestBody Map<String,String>map){
        //1.获取参数
        String movementId = map.get("movementId");
        String comment = map.get("comment");
        //2.调用service处理业务
        commentsService.saveComment(movementId,comment);
        //3.返回处理结果
        return ResponseEntity.ok(null);
    }

    @GetMapping("/{id}/like")//对评论进行点赞
    public  ResponseEntity like(@PathVariable("id")String id){
        //1.接收参数,参数为互动id
        //2.调用service处理业务
        Integer likeCount=commentsService.like(id);
        //3.返回参数,前端需要点赞数
        return ResponseEntity.ok(likeCount);
    }

    @GetMapping("/{id}/dislike")//取消对评论的点赞
    public ResponseEntity disLike(@PathVariable("id")String id ){
        //1.接收参数,参数是评论的id
        //2.调用service处理业务逻辑
        Integer count=commentsService.disLike(id);
        //3.返回结果
        return ResponseEntity.ok(count);
    }
}
